package com.example.tellwin.home.adpater;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tellwin.R;
import com.example.tellwin.home.bean.AnswerQuestionBean;
import com.example.tellwin.home.bean.QuestionBean;
import com.example.tellwin.view.ContentTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : LinZePeng
 * @date : 2020/11/19
 * @Describe ：
 **/
public class QuestionAdapter extends RecyclerView.Adapter<QuestionAdapter.QuestionViewHolder> {

    private List<AnswerQuestionBean> questionBeans = new ArrayList<>();
    Context context;
    OnQuestionItemClickListener onQuestionItemClickListener;

    public void setOnQuestionItemClickListener(OnQuestionItemClickListener onQuestionItemClickListener) {
        this.onQuestionItemClickListener = onQuestionItemClickListener;
    }

    public void setQuestionBeans(List<AnswerQuestionBean> questionBeans) {
        this.questionBeans = questionBeans;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public QuestionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new QuestionViewHolder(LayoutInflater.from(context).inflate(R.layout.item_quesiton, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull QuestionViewHolder holder, int position) {
        Resources resources = context.getResources();
        if(position%3==0){
            holder.questionItemView.setBackgroundResource(R.drawable.bg_blue);
        }else if (position%3==1){
            holder.questionItemView.setBackgroundResource(R.drawable.bg_green);
        }else {
            holder.questionItemView.setBackgroundResource(R.drawable.bg_yellow);
        }
        holder.questionItemView.setPadding(resources.getDimensionPixelOffset(R.dimen.dp_15)
                ,resources.getDimensionPixelOffset(R.dimen.dp_17)
                ,resources.getDimensionPixelOffset(R.dimen.dp_15)
                ,resources.getDimensionPixelOffset(R.dimen.dp_17));
        AnswerQuestionBean questionBean = questionBeans.get(position);
        holder.questionTitleTv.setText(questionBean.getAskTitle());
        holder.orderNumberTv.setTvStr(questionBean.getOrderId());
        holder.quesitonTimeTv.setTvStr(questionBean.getCreateTime());
        holder.questionPeopleTv.setTvStr(questionBean.getRealName());
        holder.channelTv.setTvStr(questionBean.getGradeName());
      //  holder.answerModeTv.setTvStr(questionBean.get());
        holder.answerModeTv.setVisibility(View.GONE);
        holder.questionItemView.setOnClickListener(v -> {
            if(onQuestionItemClickListener!=null){
                onQuestionItemClickListener.onQuestionItemClick(position, questionBean);
            }
        });
    }

    @Override
    public int getItemCount() {
        return questionBeans.size();
    }

    class QuestionViewHolder extends RecyclerView.ViewHolder {

        LinearLayout questionItemView;
        TextView questionTitleTv;
        ContentTextView orderNumberTv;
        ContentTextView quesitonTimeTv;
        ContentTextView questionPeopleTv;
        ContentTextView channelTv;
        ContentTextView answerModeTv;

        public QuestionViewHolder(@NonNull View itemView) {
            super(itemView);
            questionTitleTv = (TextView) itemView.findViewById(R.id.question_title_tv);
            orderNumberTv = (ContentTextView) itemView.findViewById(R.id.order_number_tv);
            quesitonTimeTv = (ContentTextView) itemView.findViewById(R.id.quesiton_time_tv);
            questionPeopleTv = (ContentTextView) itemView.findViewById(R.id.question_people_tv);
            channelTv = (ContentTextView) itemView.findViewById(R.id.channel_tv);
            answerModeTv = (ContentTextView) itemView.findViewById(R.id.answer_mode_tv);
            questionItemView = itemView.findViewById(R.id.question_item_view);
        }
    }

    public interface OnQuestionItemClickListener{
        void onQuestionItemClick(int position , AnswerQuestionBean answerQuestionBean);
    }
}
